package cn.evolution.retryutils.configuration;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author evolution
 * @title: ScheduleConfig
 * @projectName retry-utils
 * @description: TODO
 * @date 2022-11-17 10:48
 * @ltd：思为
 */
@Slf4j
@Configuration
@EnableScheduling
public class ScheduleConfig {
    
    @Bean
    public ThreadPoolTaskScheduler retryThreadPoolTaskScheduler(){
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.setPoolSize(10);
        threadPoolTaskScheduler.setRemoveOnCancelPolicy(true);
        threadPoolTaskScheduler.setAwaitTerminationMillis(30);
        threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true);
        threadPoolTaskScheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        threadPoolTaskScheduler.setErrorHandler(t ->log.error(t.toString()));
        threadPoolTaskScheduler.initialize();
        return threadPoolTaskScheduler;
        
    }
}
